class EntityM {
    constructor() {}
    add() {
        var plotName = $("#queryEntityName_text").val().replaceAll(" ", "");
        if(!plotName){
            showMsgLayer("请先输入名称！");
        }else{
            if (EntityM.handlerPoint) {
                EntityM.handlerPoint.destroy();
                EntityM.handlerPoint = null;
            }
            EntityM.handlerPoint = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
            //左键添加
            EntityM.handlerPoint.setInputAction(function(event) {
                layer.confirm("确认保存吗？", { icon: 3, title: '提示' }, function(index) {
                    var cartesian = getPickPositionGlobe(event.position);
                    EntityM.postEntityData(cartesian, plotName, "");
                    //EntityM.addEntitybyModal(cartesian);
                    EntityM.handlerPoint.destroy();
                    layer.close(index);
                });
                
            }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            //右键取消
            EntityM.handlerPoint.setInputAction(function() {
                $('#entity_modal').modal('hide');
                EntityM.handlerPoint.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
                EntityM.handlerPoint.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        }
    }
    static postEntityData(position, name, description) {
        $.ajaxSettings.async = false;
        $.ajax({
            type: "post",
            async: false,
            crossDomain : true,
            xhrFields : {
                withCredentials: true
            },
            url: EntityM.ajax + "addCollection",
            data: {
                type: "plot",
                name: name,
                info: JSON.stringify({
                    //username: Login.UserName(),
                    id: getUUID(),
                    position: position.toString().substr(1, position.toString().length - 2),
                    plotname: name,
                    //description: description
                })
            },
            datatype: "json",
            success: function (result) {
                result = JSON.parse(result)
                if (result.code == 200) {
                    showMsgLayer("保存成功");
                    //$('#entity_modal').modal('hide');
                    EntityM.getEntitysDataByName("");
                }
            }
        });
    }
    static getEntitysDataByName(token) {
        if(!EntityM.ajax){
            EntityM.ajax = getBaseplatformPort() + "map/"
        }
        $.ajaxSettings.async = false;
        $.ajax({
            type: "post",
            async: false,
            crossDomain : true,
            xhrFields : {
                withCredentials: true
            },
            url: EntityM.ajax + "getCollection",
            data: {
                type: "plot"
            },
            datatype: "json",
            success: function (result) {
                result = JSON.parse(result);
                if (result.code == 200) {
                    userPlotList = result.data;
                } else {
                    showMsgLayer("获取标签数据列表失败！");
                }
            }
        });
        var  html="";
        if (userPlotList && userPlotList.length > 0) {
            for (var i = 0; i < userPlotList.length; i++) {
                var position = JSON.parse(userPlotList[i].INFO).position;
                var name = userPlotList[i].NAME;
                var plotId = userPlotList[i].ID;
                //var LableData = data[i];
                html += '<tr>' +
                    '<td style="">' + name + '</td>' +
                    '<td style=""><button class="btn btn-xs entityHandle"><i class="layui-icon">&#xe715;</i></button>' +
                    '<button class="btn btn-xs entityDelete" style="margin-left: 15px;"><i class="layui-icon">&#x1007;</i></button></td>' +                 
                    "<td style='display:none;'><a style='color:black;text-decoration:none;font-size:13px;'>" + plotId+ "</a></td>" +
                    "<td style='display:none;'><a style='color:black;text-decoration:none;font-size:13px;'>" + position + "</a></td>" +
                    '</tr>';
                //EntityM.addEntity(cartesian3, name, description)
            }
            $("#LablePage_table tbody").html(html);
        } else {
            $("#LablePage_table tbody").html(html);
            //showMsgLayer("未获取到标绘信息");
        }
        $(".entityDelete").click(function () {
            var trSeq = $(this).parent().parent().parent().find("tr").index($(this).parent().parent()); //选中的哪行
            var flyId = $("#LablePage_table tr:gt(0):eq(" + trSeq + ") td:eq(2)").text(); //获取选中行的id列值
            $.ajaxSettings.async = false;
            $.ajax({
                type: "post",
                async: false,
                url: EntityM.ajax + "deleteCollection",
                data: {
                    type: "plot",
                    id: flyId
                },
                crossDomain : true,
                xhrFields : {
                    withCredentials: true
                },
                datatype: "json",
                success: function (result) {
                    result = JSON.parse(result)
                    if (result.code == 200) {
                        showMsgLayer("删除成功！");
                        for (var i = 0; i < viewer.entities.values.length;) {
                            var entityId = viewer.entities.values[i].id;
                            if (entityId && entityId.indexOf("_userplot") > -1) {
                                viewer.entities.remove(viewer.entities.values[i]);
                            } else {
                                i++;
                            }
                        }
                        EntityM.getEntitysDataByName("");
                    } else {
                        showMsgLayer("删除失败！");
                    }
                }
            });
        });
        $(".entityHandle").click(function () {
            for (var i = 0; i < viewer.entities.values.length;) {
                var entityId = viewer.entities.values[i].id;
                if (entityId && entityId.indexOf("_userplot") > -1) {
                    viewer.entities.remove(viewer.entities.values[i]);
                } else {
                    i++;
                }
            }
            var trSeq = $(this).parent().parent().parent().find("tr").index($(this).parent().parent()); //选中的哪行
            var position = $("#LablePage_table tr:gt(0):eq(" + trSeq + ") td:eq(3)").text().split(","); //获取选中行的id列值
            var id = $("#LablePage_table tr:gt(0):eq(" + trSeq + ") td:eq(2)").text(); //获取选中行的id列值
            var name = $("#LablePage_table tr:gt(0):eq(" + trSeq + ") td:eq(0)").text(); //获取选中行的id列值
            var cartesian3 = new Cesium.Cartesian3(parseFloat(position[0].replace(" ", "")), parseFloat(position[1].replace(" ", "")), parseFloat(position[2].replace(" ", "")));
            var pinBuilder = new Cesium.PinBuilder();
            var plotentity = viewer.entities.add({
                id: id + "_userplot",
                name: id + "_userplot",
                position: cartesian3,
                //billboard: {
                //    image: pinBuilder.fromText(name, Cesium.Color.BLUE.withAlpha(0.75), 48).toDataURL(),
                //    distanceDisplayCondition: 2000,
                //    verticalOrigin: Cesium.VerticalOrigin.BOTTOM
                //}
                point : {
                    heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    pixelSize: 5,
                    color: Cesium.Color.RED.withAlpha(1)
                }
            });
            var position3D = Cesium.Cartographic.fromCartesian(cartesian3)
            var longitude = Cesium.Math.toDegrees(position3D.longitude);
            var latitude = Cesium.Math.toDegrees(position3D.latitude);
            MapStatic.flyTo({ lon: longitude, lat: latitude, height: 2000 });
        });
    }
}